import requests
from lxml import etree
import pohan
from pohan.pinyin.pinyin import Style
class Name:
    def __init__(self,address):
        self.address=address
    def check_name(self):  #得到所有研究生导师名字
        #url
        url=self.address
        #headers
        headers = {
                'accept-language': 'zh-CN,zh;q=0.9',
                'cache-control': 'max-age=0',
                'cookie': 'Hm_lvt_c3acb27768b401b6598a1ae2797371a4=1693548649,1693581545; '
                          'Hm_lpvt_c3acb27768b401b6598a1ae2797371a4=1693581548',
                'sec-fetch-dest': 'document',
                'sec-fetch-mode': 'navigate',
                'sec-fetch-site': 'same-origin',
                'sec-fetch-user': '?1',
                'upgrade-insecure-requests': '1',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
                              '537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/'
                              '537.36 SLBrowser/7.0.0.6251 SLBChan/124'}

        #获取响应
        response=requests.get(url=url,headers=headers)
        content=response.text
        #xpath
        tree=etree.HTML(content)
        Big_name_list=tree.xpath('//div[@class="content pad10"]/table/tbody/tr/'
                                 'td[@style="width:152px;height:22px;"]/p/text()')
        Name_list=[]
        for name in Big_name_list:
            if  name != '\xa0':
                Name_list.append(name)
        Fina_teacher_name = []
        Final_teacher_name=[]
        for name in Name_list:
            # name为str
            na_list = name.split(sep="、")
            Fina_teacher_name.extend(na_list)
        #实现列表名字去重

        for name in Fina_teacher_name:
            if name not in Final_teacher_name:
                Final_teacher_name.append(name)
        return Final_teacher_name

    def get_all_name(self,path): ##path为xpath表达式,这是得到每一页所有导师的名字
        url = self.address      #url为一页的url
        # headers
        headers = {
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'max-age=0',
            'cookie': 'Hm_lvt_c3acb27768b401b6598a1ae2797371a4=1693548649,1693581545; '
                      'Hm_lpvt_c3acb27768b401b6598a1ae2797371a4=1693581548',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
                          '537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/'
                          '537.36 SLBrowser/7.0.0.6251 SLBChan/124'}

        # 获取响应
        response = requests.get(url=url, headers=headers)
        content = response.content
        # xpath
        tree = etree.HTML(content)
        all_name = tree.xpath(path)
        return all_name


    def get_pingyingnanme(self):
        Final_teacher_name=self.check_name()
        #Final_teacher_name = []

        pingying_list = []
        count = len(Final_teacher_name)
        for i in range(count):
            name = Final_teacher_name[i]
            n = pohan.pinyin.han2pinyin(name, style=Style.NORMAL)
            str = ''
            for i in n:
                str = str + i[0]
            pingying_list.append(str)
        return (pingying_list)


if __name__ == '__main__':
    #数媒研究生导师老师的名字
   shumei=Name("https://yzb.bupt.edu.cn/content/content.php?p=8_4_74")
   shumei_name=shumei.check_name()

     #数媒所有老师的名字
   shuemi_name=Name("https://sdmda.bupt.edu.cn/szdw/js.htm")
   a1=shuemi_name.get_all_name('//div[@class="name-group"]/span[1]/text()')
   shuemi_name=Name("https://sdmda.bupt.edu.cn/szdw/fjs.htm")
   a2=shuemi_name.get_all_name('//div[@class="name-group"]/span[1]/text()')  #第二张
   shuemi_name = Name("https://sdmda.bupt.edu.cn/szdw/js1.htm")
   a3 = shuemi_name.get_all_name('//div[@class="name-group"]/span[1]/text()')  # 第二张
   shuemi_name = Name("https://sdmda.bupt.edu.cn/szdw/xz.htm")
   a4 = shuemi_name.get_all_name('//div[@class="name-group"]/span[1]/text()')
   shuemi_name = Name("https://sdmda.bupt.edu.cn/szdw/jf.htm")
   a5 = shuemi_name.get_all_name('//div[@class="name-group"]/span[1]/text()')

   a=a1+a2+a3+a4+a5
   print(a)